package com.example.lib_cache.viewmodel;

import android.content.Context;
import android.util.Log;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.search.route.DrivingRouteLine;
import com.example.lib_cache.model.Address;
import com.example.lib_cache.model.EndPoi;
import com.example.lib_cache.model.Navigation;
import com.example.lib_cache.model.Poi;
import com.example.lib_cache.network.exception.ApiException;
import com.example.lib_cache.network.exception.ErrorConsumer;
import com.example.lib_cache.network.response.ResponseResultTransformer;
import com.example.lib_cache.service.ApiService;
import com.example.lib_common.util.LiveDataManager;

import io.reactivex.functions.Consumer;

public class RoutePlanActivityViewModel extends ViewModel {

    private final static String TAG = RoutePlanActivityViewModel.class.getSimpleName();

    private MutableLiveData<Poi> poiMutableLiveData;

    private MutableLiveData<EndPoi> endPoiMutableLiveData;

    private MutableLiveData<Address> addressMutableLiveData;

    private DrivingRouteLine drivingRouteLine;

    private MutableLiveData<Boolean> apiResult = new MutableLiveData<>();


    public MutableLiveData<Address> getAddressMutableLiveData(){
        return addressMutableLiveData;
    }

    public void setAddressMutableLiveData(Address address  ){
        this.addressMutableLiveData = new MutableLiveData<>();
        this.addressMutableLiveData.setValue(address);
    }

    public MutableLiveData<EndPoi> getEndPoiMutableLiveData() {
        return endPoiMutableLiveData;
    }

    public void setEndPoiMutableLiveData(EndPoi poi) {
        this.endPoiMutableLiveData = new MutableLiveData<>();
        this.endPoiMutableLiveData.setValue(poi);
    }

    public MutableLiveData<Poi> getPoiMutableLiveData() {
        return poiMutableLiveData;
    }

    public void setPoiMutableLiveData(Poi poi) {
        this.poiMutableLiveData = new MutableLiveData<>();
        this.poiMutableLiveData.setValue(poi);
    }

    public void getPoiInfo(){
        setPoiMutableLiveData(LiveDataManager
                .getInstance().with("PoiInfo",Poi.class)
                .getValue());
    }

    public void getEndPoiInfo(){
        setEndPoiMutableLiveData(LiveDataManager
                .getInstance().with("EndPoiInfo",EndPoi.class)
                .getValue());
    }

    public MutableLiveData<Boolean> getApiResult() {
        return apiResult;
    }

    public void setApiResult(boolean result) {
        this.apiResult.postValue(result);
    }

    public void loadingData() {

        setAddressMutableLiveData(LiveDataManager
                .getInstance().with("address",Address.class)
                .getValue());

        setPoiMutableLiveData(LiveDataManager
                .getInstance().with("PoiInfo",Poi.class)
                .getValue());

        setEndPoiMutableLiveData(LiveDataManager
                .getInstance().with("EndPoiInfo",EndPoi.class)
                .getValue());


    }

    public void loadingRoute(DrivingRouteLine drivingRouteLine) {
        this.drivingRouteLine = drivingRouteLine;
    }


    public void sendingOrder(ApiService apiService, Context context,int num) {
        if(null==drivingRouteLine){
            return;
        }
        Navigation navigation= new Navigation();
        navigation.setsName(poiMutableLiveData.getValue().getPoiName());
        navigation.seteName(endPoiMutableLiveData.getValue().getPoiName());
        navigation.setDistance(drivingRouteLine.getDistance());
        navigation.setDuratino(drivingRouteLine.getDuration());
        navigation.setCity(addressMutableLiveData.getValue().getCity());
        navigation.setStartLongitude(drivingRouteLine.getStarting().getLocation().longitude);
        navigation.setStartLatitude(drivingRouteLine.getStarting().getLocation().latitude);
        navigation.setEndLongitude(drivingRouteLine.getTerminal().getLocation().longitude);
        navigation.setEndLatitude(drivingRouteLine.getTerminal().getLocation().latitude);
        navigation.setsLogitude(poiMutableLiveData.getValue().getLatLng().longitude);
        navigation.setsLatitude(poiMutableLiveData.getValue().getLatLng().latitude);
        navigation.seteLogitude(endPoiMutableLiveData.getValue().getLatLng().longitude);
        navigation.seteLatitude(endPoiMutableLiveData.getValue().getLatLng().latitude);

        apiService.sendingOrder(navigation,num)
                .compose(ResponseResultTransformer.<String>obtain())
                .subscribe(new Consumer<String>() {
                    @Override
                    public void accept(String s) throws Exception {
                        Log.d(TAG,"发送成功"+s);
                        apiResult.postValue(true);
                    }
                }, new ErrorConsumer(context) {
                    @Override
                    protected void error(ApiException ex) {
                        Log.d(TAG,"发送失败"+ex.getMessage());
                        Log.d(TAG,"ErrorMsg:"+ex.getErrorMsg());
                    }
                });
    }
}
